package com.wanmait.recycle.webcontroller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Answer;
import com.wanmait.recycle.pojo.Article;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Question;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.redis.dao.RedisPraiseAnswerDao;
import com.wanmait.recycle.service.AnswerService;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.QuestionService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.IPUtil;


@Controller
@RequestMapping("web/")
public class WebQuestionController {

	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private RubbishService rubbishService;
	@Resource
	private QuestionService questionService;
	@Resource
	private AnswerService answerService;
	@Resource
	private RedisPraiseAnswerDao redisPraiseAnswerDao;
	@Resource
	private UserinfoService userinfoService;
	
	
	@RequestMapping("question/list")
	public ModelAndView list()
	{
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		
		PageInfo<Question> questions= questionService.findAllByPage();
		mv.addObject("questions",questions);
		List<Firsttype> s1 = new ArrayList<>();
		for (Firsttype firsttype : firsttypes) {
			if (firsttype.getSecondtypes().size()>0) {
				s1.add(firsttype);
			}
		}
		mv.addObject("firsttypes", s1);
		return mv;
	}
	
	@RequestMapping("question/findAllByFirstTypeId")
	@ResponseBody
	public List<Secondtype> findAllByFirstTypeId(Integer firstTypeId)
	{
		List<Secondtype> secondtypes = secondtypeService.findAllByFirsttypeId(firstTypeId);
		List<Secondtype> s1 = new ArrayList<>();
		for (Secondtype secondtype : secondtypes) {
			if (secondtype.getRubbishs().size()>0) {
				s1.add(secondtype);
			}
		}
		return s1;
	}
	
	@RequestMapping("question/findAllFirsttype")
	public List<Firsttype> findAllFirsttype(){
		List<Firsttype> firsttypes = firsttypeService.findAll();
		return firsttypes;
	}
	
	@RequestMapping("question/findAllBySecondTypeId")
	@ResponseBody
	public List<Rubbish> findAllBySecondTypeId(Integer secondTypeId)
	{
		
		List<Rubbish> rubbishs = rubbishService.findAllBySecondTypeId(secondTypeId);
		return rubbishs;
	}
	
	@RequestMapping("question/chaXun")
	public  ModelAndView chaXun(Integer rubbishId){
		ModelAndView mv = new ModelAndView();
		Rubbish rubbish = rubbishService.selectById(rubbishId);
		PageInfo<Question> questions = questionService.findAllByRubbishId(rubbishId);
		List<Firsttype> firsttypes = firsttypeService.findAll();
		List<Firsttype> s1 = new ArrayList<>();
		for (Firsttype firsttype : firsttypes) {
			if (firsttype.getSecondtypes().size()>0) {
				s1.add(firsttype);
			}
		}
		mv.addObject("firsttypes", s1);
		mv.addObject("questions", questions);
		mv.addObject("rubbishId", rubbishId);
		mv.addObject("rubbish", rubbish);
		return mv;
	}
	

	@RequestMapping("question/questionSingle")
	public ModelAndView questionSingle(Integer questionId,HttpSession session,HttpServletRequest request)
	{
		ModelAndView mv = new ModelAndView();
		Question questions =  questionService.findById(questionId);
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");                                                                                   
		if(userinfo!=null) {
			List<Answer> answers = questions.getAnswers();
			for (Answer answer : answers) {
				answer.setIsPraise(redisPraiseAnswerDao.isPraise(answer.getId(), userinfo.getId()));     
			}
		}
		Rubbish rubbishs = questions.getRubbish();
		mv.addObject("rubbishs", rubbishs);
		mv.addObject("questions", questions);
		boolean isCollect = false; //问题是否被当前用户收藏
		boolean isViewQuestion = false;	//是否浏览过，如果没有，则记录一次（如果浏览过，时间超过24小时，也记录一次）
		//获得该次请求的ip
		String ip = IPUtil.getIpAddr(request);
		isViewQuestion = questionService.isViewQuestion(questionId, ip);
		if(userinfo!=null) {
			isCollect = questionService.isCollect(questionId, userinfo.getId());
		}
		
		mv.addObject("isCollect", isCollect);
		if(isViewQuestion) {
			userinfoService.updateCount(userinfo);
			questionService.addViewQuestion(questionId, ip);
		}
		
		return mv;
		
	}
	
	/*收藏*/
	@RequestMapping("question/addCollect")
	@ResponseBody
	public void addCollect(Integer questionId,HttpSession session) {
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		questionService.addCollect(questionId, userinfo.getId());
	}
	
	/*取消收藏*/
	@RequestMapping("question/removeCollect")
	@ResponseBody
	public void removeCollect(Integer questionId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		questionService.removeCollect(questionId, userinfo.getId());
	}
	
	/*点赞*/
	@RequestMapping("question/addPraise")
	@ResponseBody
	public void addPraise(Integer answerId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		answerService.addPraise(answerId, userinfo.getId());
	}
	
	/*取消点赞*/
	@RequestMapping("question/removePraise")
	@ResponseBody
	public void removePraise(Integer answerId,HttpSession session) {
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		answerService.removePraise(answerId, userinfo.getId());
	}
	
	@RequestMapping("question/addAnswer")
	public String addAnswer(Answer answer,HttpSession session)
	{
		Userinfo userinfo =  (Userinfo)session.getAttribute("userinfo");
		answerService.addAnswer(answer);
		userinfoService.updateCount(userinfo);
		return "redirect:/web/question/questionSingle?questionId="+answer.getQuestion().getId();
	}
	
	@RequestMapping("question/addQList")
	public String addQList(Question question)
	{
		if(question.getContent()!=null)
		{
			
			questionService.addQuestion(question);
			return "redirect:/web/question/list";
		}
		return null;
	}
	
	@RequestMapping("question/addQ")
	public String addQ(HttpSession session)
	{
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			userinfoService.updateCount(userinfo);
			return "redirect:/web/login";
		}
		
		return null;
	}
		
	@RequestMapping("accept")
	@ResponseBody
	public ModelAndView accept(Integer id) {
		
		ModelAndView mv = new ModelAndView();
		Rubbish rubbish = rubbishService.selectById(id);
		
		mv.addObject("rubbish", rubbish);
		
		mv.setViewName("/web/includ/addQList");
		
		return mv;
	}
	
	@RequestMapping("userinfo/qList")
	@ModelAttribute("question")
	public ModelAndView qList(HttpSession session)
	{
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		PageInfo<Question> questions=null;
		try {
			questions = questionService.findAllByUserId(userinfo.getId());
		} catch (Exception e) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}
		mv.addObject("questions", questions);
		return mv;
	}
	
	@RequestMapping("userinfo/chaXunById")
	public  ModelAndView chaXunById(HttpSession session, Integer rubbishId){
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		ModelAndView mv = new ModelAndView();
		PageInfo<Question> questions = questionService.findAllByRubbishIdAndUserinfoId(rubbishId,userinfo.getId());
		List<Firsttype> firsttypes = firsttypeService.findAll();
	
		mv.addObject("firsttypes", firsttypes);
		mv.addObject("questions", questions);
		return mv;
	}
	
	@RequestMapping("userinfo/answerIsPick")
	@ResponseBody
	public  boolean answerIsPick(Integer questionId,Integer answerId,HttpSession session){
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		Answer answer = new Answer();
		answer.setId(answerId);
		Question question = new Question();
		question.setId(questionId);
		answer.setUserinfo(userinfo);
		answer.setQuestion(question);
		answerService.isPick(answer);
		return true;
	}
	
	@RequestMapping("question/listByTime")
	public ModelAndView listByTime()
	{
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		PageInfo<Question> questions= questionService.findAllByPageAndTime();
		mv.addObject("questions",questions);
		mv.setViewName("/web/question/list");
		return mv;
	}
	
	@RequestMapping("question/reDu")
	public ModelAndView reDu()
	{
		
		ModelAndView mv = new ModelAndView();
		List<Firsttype> firsttypes = firsttypeService.findAll();
		mv.addObject("firsttypes", firsttypes);
		PageInfo<Question> questions= questionService.findAllByPageAndRedu();
		mv.addObject("questions",questions);
		mv.setViewName("/web/question/list");
		return mv;
	}
	/*某个用户收藏的文章*/
	@RequestMapping("/userinfo/myQuestionCollect")
	public ModelAndView collectQuestion(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			mv.addObject("msg", "login");
			mv.setViewName("redirect:/index.html");
		}else {
			PageInfo<Question> pageInfo = null;
			
			pageInfo = questionService.findCollectQuestion(userinfo.getId());
			mv.addObject("pageInfo", pageInfo);
		}
		return mv;
	}
	
}







